<?php

class Message extends La_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model(array( 'User_model','Message_model','Log_model'));
		if (!$this->authorize->is_admin_logged_in()) {
			la_redirect("/admin/login", "请先登录");
		}
		$this->data['nav'] = 'message';
	}
	
	function index() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$msg_to = $this->input->post('msg_to');
			
			$group_array = array("all" => -1, "identified" => -2, "expert" => -3, "normal" => -4);
			if (!array_key_exists($msg_to, $group_array)) {
				$user_to = $this->User_model->find_user_by_nick($msg_to);
				if(empty($user_to)) {
					la_redirect("/console/message", "接收用户不存在");
					die(0);
				}
				$user_to_id = $user_to['id'];
				$log_type = "message";
			} else {
				$user_to_id = $group_array[$msg_to];
				$log_type = "message_group";
			}
			$post_data['msg_to_id'] = $user_to_id;
			$post_data['subject'] = $this->input->post('subject');
			$post_data['content'] = $this->input->post('content');
			if(empty($post_data['subject']) || empty($post_data['content'])) {
					la_redirect("/console/message", "标题和内容为空");
					die(0);
				}
			$post_data['msg_from_id'] = 0;
			$post_data['msg_from'] = "系统管理员";
			$post_data['created'] = date("Y-m-d H:i:s");
			$post_data['related_id'] = 0;
			$post_data['is_system'] = 1;
			$this->load->model('Message_model');
			$message_id = $this->Message_model->add($post_data);
			$params = json_encode(array_merge($post_data,array('msg_to' => $msg_to, "message_id" => $message_id)));
			$data = array('params' =>$params,'type' => $log_type, 'created_by' =>  $this->authorize->current_admin_user(), 'created' => date("Y-m-d H:i:s"));
			$this->Log_model->add($data);
			la_redirect("/console/message/records", "发布成功");
		} else {
			$this->load->view('admin/send_message', $this->data);
		}
	}	
	
	
	public function records() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$type = $this->input->post("type");
			if(empty($type)) {
					$type = "all";
			}
			$ids = $this->input->post("ids");
			if (empty($ids)) {
				redirect("/console/message");
			}
			foreach ($ids as $id) {
				$log = $this->Log_model->find_by_id(intval($id));
				$params = json_decode($log['params']);
				$message_id = $params->message_id;
				$this->Message_model->delete($message_id);
				$this->Log_model->delete($id);
			}
			redirect("/console/message/records?type=" . $type);
		} else {
			$offset = intval($this->uri->segment(4));
			$type = $this->input->get("type");
			if(empty($type)) {
				$type = "all";
			}
			switch ($type) {
				case "group":
						$sql = " and l.type = 'message_group'";
					break;
				case "person":
					$sql = " and l.type = 'message'";
					break;
				case "all":
					$sql = " and (l.type = 'message' or l.type = 'message_group')";
				default:
					break;
			}
			$count = $this->Log_model->get_count_by_condition($sql);
			$this->load->library('pager');
			$config['base_url'] = site_url("console/message/records/");
			$config['total_rows'] = $count;
			$config['per_page'] = LA_PAGE_SIZE;
			$config['uri_segment'] = 4;
			$this->pager->initialize($config);
			$logs = $this->Log_model->find_by_condition($sql, $offset, $config['per_page']);
			$this->data['logs'] = $logs;
			$this->data['total'] = $count;
			$this->data['pager'] =  $this->pager->create_links();
			$this->data['type'] = $type;
			$this->load->view('admin/system_message', $this->data);
		}
	}
}